package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.OmcUserVo;
import com.csun.cmny.provider.service.OmcGroupFeignApi;
import com.csun.cmny.provider.service.OmcOldmanFeignApi;
import com.csun.cmny.provider.service.OmcRpcService;
import com.csun.cmny.util.wrapper.Wrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
@Transactional
public class OmcRpcServiceImpl implements OmcRpcService {

    @Resource
    private OmcGroupFeignApi omcGroupFeignApi;
    @Resource
    private OmcOldmanFeignApi omcOldmanFeignApi;

    @Override
    public Integer deleteNursingUserBind(Long userId) {
        try {
            Wrapper<Integer> wrapper = omcGroupFeignApi.deleteNursingUserBind(userId);

            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.OMC10036007);
            }
            if(wrapper.getResult().equals(0)){
                throw new OmcBizException(ErrorCodeEnum.OMC10036006);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("getUserId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public OmcUserVo getUserNursingGroupInfo(Long userId) {
        try {
            Wrapper<OmcUserVo> wrapper = omcGroupFeignApi.getUserNursingGroupInfo(userId);

            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.GL9999404);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("getUserId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public OldmanBedRoomBuildingVo queryOldmanBedRoomBuildingByOldmanId(Integer oldmanId) {

        try {
            Wrapper<OldmanBedRoomBuildingVo> wrapper = omcOldmanFeignApi.queryOldmanBedRoomBuildingByOldmanId(oldmanId);
            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610063);
            }

            OldmanBedRoomBuildingVo result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }

        return null;
    }

    @Override
    public List<Long> selectStaffIdBySpecialist() {

        try {
            Wrapper<List<Long>> wrapper = omcOldmanFeignApi.selectStaffIdBySpecialist();

            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.DMC100610063);
            }

            List<Long> result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            log.error("getOldmanByOldmanId={}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer selectHaveUnfinishedOrderNumbers(Long userId) {
        try {

            Wrapper<Integer> wrapper = omcOldmanFeignApi.selectHaveUnfinishedOrderNumbers(userId);

            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.UAC10017007);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10017007);
            }

            Integer result = wrapper.getResult();



            return result;
        } catch (Exception e) {
            throw new OmcBizException(ErrorCodeEnum.UAC10017007);
        }

    }

    @Override
    public Integer isSpecialist(Long id) {
        try {
            Wrapper<Integer> wrapper = omcOldmanFeignApi.isSpecialist(id);

            if (wrapper == null) {
                throw new OmcBizException(ErrorCodeEnum.UAC10017008);
            }
            if (wrapper.error()) {
                throw new OmcBizException(ErrorCodeEnum.UAC10017008);
            }

            Integer result = wrapper.getResult();

            assert result != null;

            return result;
        } catch (Exception e) {
            throw new OmcBizException(ErrorCodeEnum.UAC10017008);
        }

    }
}
